package com.ymttest.database.sqlwapper.sqdistributor;

import java.util.HashMap;
import java.util.List;

import com.ymt.database.SqlManager;
import com.ymttest.database.model.sqdistributor.UserCpsEvent;
import com.ymttest.utils.YMTDateUtil;

public class UserCpsEventWrapper {
	
	SqlManager manager;

	public UserCpsEventWrapper() {
		manager = new SqlManager("sqdistributor");
	}

	@SuppressWarnings("unchecked")
	public UserCpsEvent selectByPrimaryKey(Long id) {

		UserCpsEvent userCpsEvent = null;
		List<UserCpsEvent> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqdistributor.UserCpsEventMapper.selectByPrimaryKey",
						id);
		manager.commit();

		if (result.size() > 0) {
			userCpsEvent = result.get(0);
		}

		return userCpsEvent;
	}
	
	@SuppressWarnings("unchecked")
	public UserCpsEvent select24HEarlistByUserId(Long userId, String addTime) {

		UserCpsEvent userCpsEvent = null;
		
		HashMap<String, Object> map = new HashMap<>();
		
		map.put("user_id", userId);
		map.put("add_time", addTime);
		
		List<UserCpsEvent> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqdistributor.UserCpsEventMapper.select24HEarlistByUserId",
						map);
		manager.commit();

		if (result.size() > 0) {
			userCpsEvent = result.get(0);
		}

		return userCpsEvent;
	}
	
	@SuppressWarnings("unchecked")
	public UserCpsEvent selectByUserIdAndDirstributorId(Long userId, Long distributorId) {

		UserCpsEvent userCpsEvent = null;
		
		HashMap<String, Long> map = new HashMap<>();
		
		map.put("user_id", userId);
		map.put("distributor_id", distributorId);
		
		List<UserCpsEvent> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqdistributor.UserCpsEventMapper.selectByUserIdAndDirstributorId",
						map);
		manager.commit();

		if (result.size() > 0) {
			userCpsEvent = result.get(0);
		}

		return userCpsEvent;
	}
	
	public int updateAddTimeByUserIdAndDistributorId(Long userId, Long distributorId) {
		
		HashMap<String, Object> map = new HashMap<>();
		
		map.put("user_id", userId);
		map.put("distributor_id", distributorId);
		map.put("add_time", YMTDateUtil.getBeforeOrNextDay(-30));
		
		int result = manager
				.update(
						"com.ymttest.database.mapping.sqdistributor.UserCpsEventMapper.updateAddTimeByUserIdAndDistributorId",
						map);
		manager.commit();

		return result;
	}
	
	@SuppressWarnings("unchecked")
	public UserCpsEvent selectByOpenIdAndDirstributorId(String openId, Long distributorId) {

		UserCpsEvent userCpsEvent = null;
		
		HashMap<String, Object> map = new HashMap<>();
		
		map.put("open_id", openId);
		map.put("distributor_id", distributorId);
		
		List<UserCpsEvent> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.sqdistributor.UserCpsEventMapper.selectByOpenIdAndDirstributorId",
						map);
		manager.commit();

		if (result.size() > 0) {
			userCpsEvent = result.get(0);
		}

		return userCpsEvent;
	}
	
	public static void main(String[] args) {
		
		UserCpsEventWrapper test = new UserCpsEventWrapper();

		System.out.println(test.select24HEarlistByUserId(25912643L, "2020-09-23 15:50:05").getDistributor_id());
	}
}
